home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP05.TXT < prev    next >
Text File  |  1989-12-30  |  29KB  |  653 lines

  1.                     Chapter 5 - Functions and variables
  2.  
  3.  
  4.                       OUR FIRST USER DEFINED FUNCTION
  5.  
  6.              Load  and examine the file SUMSQRES.C for an example of 
  7.         a C program with functions.   Actually this is not the first 
  8.         function  we have encountered because the "main" program  we 
  9.         have been using all along is technically a function,  as  is 
  10.         the  "printf" function.   The "printf" function is a library 
  11.         function that was supplied with your compiler. 
  12.  
  13.              Notice the executable part of this program.   It begins 
  14.         with a line that simply says "header()", which is the way to 
  15.         call any function.  The parentheses are required because the 
  16.         C compiler uses them to determine that it is a function call 
  17.         and not simply a misplaced variable.  When the program comes 
  18.         to this line of code, the function named "header" is called, 
  19.         its  statements  are executed,  and control returns  to  the 
  20.         statement  following this call.   Continuing on we come to a 
  21.         "for"  loop which will be executed 7 times and  which  calls 
  22.         another  function named "square" each time through the loop, 
  23.         and  finally  a function named "ending" will be  called  and 
  24.         executed.    For  the  moment  ignore  the  "index"  in  the 
  25.         parentheses of the call to "square".  We have seen that this 
  26.         program  therefore calls a header,  7 square calls,  and  an 
  27.         ending. Now we need to define the functions.
  28.  
  29.                            DEFINING THE FUNCTIONS
  30.  
  31.              Following the main program you will see another program 
  32.         that  follows all of the rules set forth so far for a "main" 
  33.         program  except that it is named "header()".   This  is  the 
  34.         function which is called from within the main program.  Each 
  35.         of  these  statements are executed,  and when they  are  all 
  36.         complete, control returns to the main program. 
  37.  
  38.              The  first  statement sets the variable "sum" equal  to 
  39.         zero because we will use it to accumulate a sum of  squares.  
  40.         Since  the  variable  "sum" is defined as  an  integer  type 
  41.         variable  prior to the main program,  it is available to  be 
  42.         used  in  any of the following functions.   It is  called  a 
  43.         "global" variable,  and it's scope is the entire program and 
  44.         all  functions.   More  will  be  said about  the  scope  of 
  45.         variables  at the end of this chapter.   The next  statement 
  46.         outputs  a header message to the monitor.   Program  control 
  47.         then  returns  to  the  main  program  since  there  are  no 
  48.         additional statements to execute in this function.
  49.  
  50.              It should be clear to you that the two executable lines 
  51.         from  this  function  could be moved to  the  main  program, 
  52.         replacing the header call,  and the program would do exactly 
  53.         the same thing that it does as it is now written.  This does 
  54.         not minimize the value of functions,  it merely  illustrates 
  55.  
  56.  
  57.                                   Page 29    
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                     Chapter 5 - Functions and variables
  68.  
  69.  
  70.         the operation of this simple function in a simple way.   You 
  71.         will find functions to be very valuable in C programming.
  72.  
  73.                        PASSING A VALUE TO A FUNCTION
  74.  
  75.              Going  back  to the main program,  and the  "for"  loop 
  76.         specifically,  we find the new construct from the end of the 
  77.         last  lesson used in the last part of the for  loop,  namely 
  78.         the "index++".   You should get used to seeing this,  as you 
  79.         will see it a lot in C programs. 
  80.  
  81.              In the call to the function "square",  we have an added 
  82.         feature, namely the variable "index" within the parentheses.  
  83.         This  is  an indication to the compiler that when you go  to 
  84.         the function,  you wish to take along the value of index  to 
  85.         use in the execution of that function.  Looking ahead at the 
  86.         function  "square",  we  find that another variable name  is 
  87.         enclosed in its parentheses,  namely the variable  "number".  
  88.         This  is  the name we prefer to call the variable passed  to 
  89.         the  function when we are in the function.   We can call  it 
  90.         anything  we wish as long as it follows the rules of  naming 
  91.         an identifier.   Since the function must know what type  the 
  92.         variable  is,  it is defined following the function name but 
  93.         before the opening brace of the function itself.   Thus, the 
  94.         line  containing "int number;" tells the function  that  the 
  95.         value  passed to it will be an integer type variable.   With 
  96.         all of that out of the way,  we now have the value of  index 
  97.         from  the main program passed to the function "square",  but 
  98.         renamed "number", and available for use within the function.
  99.  
  100.              Following the opening brace of the function,  we define 
  101.         another  variable "numsq" for use only within  the  function 
  102.         itself,  (more  about  that  later)  and  proceed  with  the 
  103.         required  calculations.   We set "numsq" equal to the square 
  104.         of  number,  then add numsq to the current total  stored  in 
  105.         "sum".   Remember  that "sum += numsq" is the same as "sum = 
  106.         sum + numsq" from the last lesson.   We print the number and 
  107.         its square, and return to the main program.
  108.  
  109.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  110.  
  111.              When we passed the value of "index" to the function,  a 
  112.         little  more  happened  than meets  the  eye.   We  did  not 
  113.         actually  pass  the  value  of index  to  the  function,  we 
  114.         actually  passed  a  copy of the value.   In  this  way  the 
  115.         original value is protected from accidental corruption by  a 
  116.         called  function.   We  could  have  modified  the  variable 
  117.         "number" in any way we wished in the function "square",  and 
  118.         when we returned to the main program, "index" would not have 
  119.         been  modified.   We thus protect the value of a variable in 
  120.         the main program from being accidentally corrupted,  but  we 
  121.  
  122.  
  123.                                   Page 30    
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                     Chapter 5 - Functions and variables
  134.  
  135.  
  136.         cannot  return  a value to the main program from a  function 
  137.         using this technique.  We will find a well defined method of 
  138.         returning  values  to  the main program or  to  any  calling 
  139.         function  when we get to arrays and another method  when  we 
  140.         get  to pointers.   Until then the only way you will be able 
  141.         to  communicate  back to the calling function will  be  with 
  142.         global  variables.    We  have  already  hinted  at   global 
  143.         variables  above,  and will discuss them in detail later  in 
  144.         this chapter.
  145.  
  146.              Continuing  in  the main program,  we come to the  last 
  147.         function call, the call to "ending".  This call simply calls 
  148.         the last function which has no local variables defined.   It 
  149.         prints out a message with the value of "sum" contained in it 
  150.         to  end the program.   The program ends by returning to  the 
  151.         main  program and finding nothing else to do.   Compile  and 
  152.         run this program and observe the output.
  153.  
  154.                         NOW TO CONFESS A LITTLE LIE
  155.  
  156.              I told you a short time ago that the only way to get  a 
  157.         value  back to the main program was through use of a  global 
  158.         variable,  but  there  is another way which we will  discuss 
  159.         after  you load and display the file  named  SQUARES.C.   In 
  160.         this  file we will see that it is simple to return a  single 
  161.         value  from a called function to the calling function.   But 
  162.         once again,  it is true that to return more than one  value, 
  163.         we will need to study either arrays or pointers.
  164.  
  165.              In the main program, we define two integers and begin a 
  166.         "for"  loop  which  will be executed  8  times.   The  first 
  167.         statement  of the for loop is "y = squ(x);",  which is a new 
  168.         and rather strange looking construct.  From past experience, 
  169.         we  should have no trouble understanding that  the  "squ(x)" 
  170.         portion  of  the statement is a call to the  "squ"  function 
  171.         taking along the value of "x" as a variable.   Looking ahead 
  172.         to  the function itself we find that the function prefers to 
  173.         call  the variable "in" and it proceeds to square the  value 
  174.         of "in" and call the result "square".   Finally,  a new kind 
  175.         of a statement appears,  the "return" statement.   The value 
  176.         within  the parentheses is assigned to the  function  itself 
  177.         and  is  returned  as a usable value in  the  main  program.  
  178.         Thus,  the  function call "squ(x)" is assigned the value  of 
  179.         the square and returned to the main program such that "y" is 
  180.         then  set  equal  to  that value.   If  "x"  were  therefore 
  181.         assigned the value 4 prior to this call,  "y" would then  be 
  182.         set to 16 as a result of this line of code. 
  183.  
  184.              Another  way  to  think  of this  is  to  consider  the 
  185.         grouping  of characters "squ(x)" as another variable with  a 
  186.         value  that is the square of "x",  and this new variable can 
  187.  
  188.  
  189.                                   Page 31    
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                     Chapter 5 - Functions and variables
  200.  
  201.  
  202.         be used any place it is legal to use a variable of its type.  
  203.         The values of "x" and "y" are then printed out.
  204.  
  205.              To  illustrate  that the grouping of  "squ(x)"  can  be 
  206.         thought  of as just another variable,  another "for" loop is 
  207.         introduced in which the function call is placed in the print 
  208.         statement rather than assigning it to a new variable.
  209.  
  210.              One  last  point must be made,  the  type  of  variable 
  211.         returned must be defined in order to make sense of the data, 
  212.         but the compiler will default the type to integer if none is 
  213.         specified.   If  any  other  type is  desired,  it  must  be 
  214.         explicitly defined.   How to do this will be demonstrated in 
  215.         the next example program.
  216.  
  217.              Compile and run this program.
  218.  
  219.                             FLOATING POINT FUNCTIONS
  220.  
  221.              Load the program FLOATSQ.C for an example of a function 
  222.         with a floating point type of return.  It begins by defining 
  223.         a global floating point variable we will use later.  Then in 
  224.         the  "main"  part  of the program,  an integer  is  defined, 
  225.         followed  by two floating point variables,  and then by  two 
  226.         strange  looking definitions.   The expressions "sqr()"  and 
  227.         "glsqr()"  look like function calls and they are.   This  is 
  228.         the proper way in C to define that a function will return  a 
  229.         value that is not of the type "int", but of some other type, 
  230.         in  this case "float".   This tells the compiler that when a 
  231.         value  is returned from either of these  two  functions,  it 
  232.         will be of type "float".
  233.  
  234.              Now  refer to the function "sqr" near the center of the 
  235.         listing and you will see that the function name is  preceded 
  236.         by the name "float".   This is an indication to the compiler 
  237.         that  this  function will return a value of type "float"  to 
  238.         any program that calls it.   The function is now  compatible 
  239.         with  the call to it.   The line following the function name 
  240.         contains  "float inval;",  which indicates to  the  compiler 
  241.         that  the variable passed to this function from the  calling 
  242.         program will be of type "float".
  243.  
  244.              The next function,  namely "glsqr",  will also return a 
  245.         "float"  type  variable,  but it uses a global variable  for 
  246.         input.   It  also does the squaring right within the  return 
  247.         statement  and  therefore has no need to define  a  separate 
  248.         variable to store the product.
  249.  
  250.              The  overall structure of this program should  pose  no 
  251.         problem and will not be discussed in any further detail.  As 
  252.  
  253.  
  254.  
  255.                                   Page 32    
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                     Chapter 5 - Functions and variables
  266.  
  267.  
  268.         is customary with all example programs, compile and run this 
  269.         program.
  270.  
  271.              There  will  be times that you will have a need  for  a 
  272.         function   to   return  a  pointer  as  a  result  of   some 
  273.         calculation.  There is a way to define a function so that it 
  274.         does  just that.   We haven't studied pointers yet,  but  we 
  275.         will soon.  This is just a short preview of things to come.
  276.  
  277.                              SCOPE OF VARIABLES
  278.  
  279.              Load the next program,  SCOPE.C,  and display it for  a 
  280.         discussion of the scope of variables in a program.
  281.  
  282.              The first variable defined is a global variable "count" 
  283.         which  is available to any function in the program since  it 
  284.         is defined before any of the functions.   In addition, it is 
  285.         always  available  because  it does not come and go  as  the 
  286.         program  is  executed.   (That  will  make  sense  shortly.) 
  287.         Farther down in the program,  another global variable  named 
  288.         "counter"  is  defined  which  is also  global  but  is  not 
  289.         available  to the main program since it is defined following 
  290.         the main program.  A global variable is any variable that is 
  291.         defined  outside of any function.   Note that both of  these 
  292.         variables  are sometimes referred to as  external  variables 
  293.         because they are external to any functions.
  294.  
  295.              Return  to  the  main  program and  you  will  see  the 
  296.         variable  "index"  defined as an integer.   Ignore the  word 
  297.         "register" for the moment.   This variable is only available 
  298.         within the main program because that is where it is defined.  
  299.         In addition, it is an "automatic" variable, which means that 
  300.         it  only comes into existence when the function in which  it 
  301.         is  contained  is  invoked,  and ceases to  exist  when  the 
  302.         function  is  finished.   This  really  means  nothing  here 
  303.         because the main program is always in operation,  even  when 
  304.         it  gives  control to another function.  Another integer  is 
  305.         defined  within  the  "for"  braces,  namely  "stuff".   Any 
  306.         pairing  of braces can contain a variable  definition  which 
  307.         will  be  valid  and  available only while  the  program  is 
  308.         executing statements within those braces.  The variable will 
  309.         be  an  "automatic" variable and will cease  to  exist  when 
  310.         execution leaves the braces.   This is convenient to use for 
  311.         a loop counter or some other very localized variable.
  312.  
  313.                        MORE ON "AUTOMATIC" VARIABLES
  314.  
  315.              Observe  the  function named "head1".   It  contains  a 
  316.         variable  named  "index",  which has nothing to do with  the 
  317.         "index" of the main program,  except that both are automatic 
  318.         variables.   When  the  program is  not  actually  executing 
  319.  
  320.  
  321.                                   Page 33    
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                     Chapter 5 - Functions and variables
  332.  
  333.  
  334.         statements  in  this function,  this variable named  "index" 
  335.         does not even exist.   When "head1" is called,  the variable 
  336.         is  generated,  and  when "head1" completes  its  task,  the 
  337.         variable  "index" is eliminated completely  from  existence.  
  338.         Keep  in mind however that this does not affect the variable 
  339.         of  the  same  name  in the main  program,  since  it  is  a 
  340.         completely separate entity.
  341.  
  342.              Automatic   variables  therefore,   are   automatically 
  343.         generated and disposed of when needed.   The important thing 
  344.         to remember is that from one call to a function to the  next 
  345.         call,  the  value of an automatic variable is not  preserved 
  346.         and must therefore be reinitialized.
  347.  
  348.                          WHAT ARE STATIC VARIABLES?
  349.  
  350.              An  additional variable type must be mentioned at  this 
  351.         point,  the "static" variable.  By putting the reserved word 
  352.         "static"  in  front  of  a  variable  declaration  within  a 
  353.         function,  the variable or variables in that declaration are 
  354.         static  variables  and will stay in existence from  call  to 
  355.         call  of  the  particular function.  
  356.  
  357.              By  putting  the  same reserved word  in  front  of  an 
  358.         external variable, one outside of any function, it makes the 
  359.         variable  private  and  not accessible to use in  any  other 
  360.         file.  This implies that it is possible to refer to external 
  361.         variables  in other separately compiled files,  and that  is 
  362.         true.  Examples of this usage will be given in chapter 14 of 
  363.         this tutorial.
  364.  
  365.                          USING THE SAME NAME AGAIN
  366.  
  367.              Refer  to  the  function named  "head2".   It  contains 
  368.         another  definition  of the variable  named  "count".   Even 
  369.         though  "count"  has  already  been  defined  as  a   global 
  370.         variable,  it  is  perfectly all right to reuse the name  in 
  371.         this  function.   It is a completely new variable  that  has 
  372.         nothing to do with the global variable of the same name, and 
  373.         causes  the  global  variable  to  be  unavailable  in  this 
  374.         function.   This allows you to write programs using existing 
  375.         functions  without worrying about what names were  used  for 
  376.         variables in the functions because there can be no conflict.  
  377.         You  only  need to worry about the variables that  interface 
  378.         with the functions.
  379.  
  380.                         WHAT IS A REGISTER VARIABLE?
  381.  
  382.              Now  to  fulfill  a promise made earlier about  what  a 
  383.         register  variable  is.   A  computer can  keep  data  in  a 
  384.         register  or  in  memory.   A  register is  much  faster  in 
  385.  
  386.  
  387.                                   Page 34    
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                     Chapter 5 - Functions and variables
  398.  
  399.  
  400.         operation  than  memory  but there are  very  few  registers 
  401.         available for the programmer to use.   If there are  certain 
  402.         variables  that are used extensively in a program,  you  can 
  403.         designate  that  those  variables  are to  be  stored  in  a 
  404.         register  if possible in order to speed up the execution  of 
  405.         the program.   Depending on the computer and the compiler, a 
  406.         small  number  of register variables may be allowed and  are 
  407.         designated  by putting the word "register" in front  of  the 
  408.         desired variable.  Check your compiler documentation for the 
  409.         availability  of  this  feature and the number  of  register 
  410.         variables.   Most  compilers that do not have  any  register 
  411.         variables available,  will simply ignore the word "register" 
  412.         and run normally, keeping all variables in memory. 
  413.  
  414.              Register  variables  are only available  for  use  with 
  415.         integer  and character type variables.   This may or may not 
  416.         include  some  of the other integer-like variables  such  as 
  417.         unsigned,  long, or short.  Check the documentation for your 
  418.         compiler.
  419.  
  420.                         WHERE DO I DEFINE VARIABLES?
  421.  
  422.              Now for a refinement on a general rule stated  earlier.  
  423.         When  you have variables brought to a function as  arguments 
  424.         to  the  function,  they are defined immediately  after  the 
  425.         function  name  and  prior  to the  opening  brace  for  the 
  426.         program.   Other  variables used in the function are defined 
  427.         at the beginning of the function,  immediately following the 
  428.         opening  brace of the function,  and before  any  executable 
  429.         statements. 
  430.  
  431.                          STANDARD FUNCTION LIBRARIES
  432.  
  433.              Every  compiler  comes  with some  standard  predefined 
  434.         functions  which  are available for  your  use.   These  are 
  435.         mostly   input/output   functions,   character  and   string 
  436.         manipulation functions,  and math functions.   We will cover 
  437.         most of these in subsequent chapters. 
  438.  
  439.              In addition,  most compilers have additional  functions 
  440.         predefined that are not standard but allow the programmer to 
  441.         get the most out of his particular computer.  In the case of 
  442.         the  IBM-PC and compatibles,  most of these functions  allow 
  443.         the  programmer  to use the BIOS services available  in  the 
  444.         operating system,  or to write directly to the video monitor 
  445.         or to any place in memory.  These will not be covered in any 
  446.         detail  as  you will be able to study the unique aspects  of 
  447.         your compiler on your own.  Many of these kinds of functions 
  448.         are used in the example programs in chapter 14.
  449.  
  450.  
  451.  
  452.  
  453.                                   Page 35    
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                     Chapter 5 - Functions and variables
  464.  
  465.  
  466.                              WHAT IS RECURSION?
  467.  
  468.              Recursion  is another of those  programming  techniques 
  469.         that  seem very intimidating the first time you come  across 
  470.         it,  but  if  you will load and display the example  program 
  471.         named RECURSON.C, we will take all of the mystery out of it.  
  472.         This  is probably the simplest recursive program that it  is 
  473.         possible  to write and it is therefore a stupid  program  in 
  474.         actual  practice,  but for purposes of illustration,  it  is 
  475.         excellent.
  476.  
  477.              Recursion  is  nothing more than a function that  calls 
  478.         itself.   It is therefore in a loop which must have a way of 
  479.         terminating.   In the program on your monitor,  the variable 
  480.         "index"  is  set to 8,  and is used as the argument  to  the 
  481.         function  "count_dn".   The function simply  decrements  the 
  482.         variable, prints it out in a message, and if the variable is 
  483.         not  zero,  it calls itself,  where it decrements it  again, 
  484.         prints it,  etc.  etc. etc. Finally, the variable will reach 
  485.         zero,  and the function will not call itself again. Instead, 
  486.         it  will  return  to the prior time it  called  itself,  and 
  487.         return  again,  until  finally it will return  to  the  main 
  488.         program and will return to DOS.
  489.  
  490.              For  purposes  of understanding you can think of it  as 
  491.         having 8 copies of the function "count_dn" available and  it 
  492.         simply  called all of them one at a time,  keeping track  of 
  493.         which  copy it was in at any given time.   That is not  what 
  494.         actually  happened,  but it is a reasonable illustration for 
  495.         you to begin understanding what it was really doing.
  496.  
  497.                               WHAT DID IT DO?
  498.  
  499.              A  better explanation of what actually happened  is  in 
  500.         order.   When you called the function from itself, it stored 
  501.         all  of the variables and all of the internal flags it needs 
  502.         to  complete the function in a block  somewhere.   The  next 
  503.         time it called itself,  it did the same thing,  creating and 
  504.         storing  another  block of everything it needed to  complete 
  505.         that  function call.   It continued making these blocks  and 
  506.         storing them away until it reached the last function when it 
  507.         started  retrieving the blocks of data,  and using  them  to 
  508.         complete  each function call.   The blocks were stored on an 
  509.         internal part of the computer called the "stack".  This is a 
  510.         part  of  memory carefully organized to store data  just  as 
  511.         described above.  It is beyond the scope of this tutorial to 
  512.         describe the stack in detail,  but it would be good for your 
  513.         programming  experience to read some material describing the 
  514.         stack.   A stack is used in nearly all modern computers  for 
  515.         internal housekeeping chores.
  516.  
  517.  
  518.  
  519.                                   Page 36    
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                     Chapter 5 - Functions and variables
  530.  
  531.  
  532.              In using recursion,  you may desire to write a  program 
  533.         with  indirect recursion as opposed to the direct  recursion 
  534.         described  above.    Indirect  recursion  would  be  when  a 
  535.         function  "A"  calls the function "B",  which in turn  calls 
  536.         "A",  etc.   This is entirely permissible,  the system  will 
  537.         take  care of putting the necessary things on the stack  and 
  538.         retrieving  them when needed again.   There is no reason why 
  539.         you  could not have three functions calling each other in  a 
  540.         circle,  or four,  or five,  etc.   The C compiler will take 
  541.         care of all of the details for you.
  542.  
  543.              The thing you must remember about recursion is that  at 
  544.         some  point,  something  must  go to  zero,  or  reach  some 
  545.         predefined  point to terminate the loop.   If not,  you will 
  546.         have  an  infinite  loop,  and the stack will  fill  up  and 
  547.         overflow,  giving  you  an error and  stopping  the  program 
  548.         rather abruptly.
  549.  
  550.                         ANOTHER EXAMPLE OF RECURSION
  551.  
  552.              The  program  named  BACKWARD.C is another  example  of 
  553.         recursion,  so load it and display it on your screen.   This 
  554.         program  is  similar to the last one except that it  uses  a 
  555.         character array.  Each successive call to the function named  
  556.         "forward_and_backward"  causes one character of the  message 
  557.         to be printed.   Additionally,  each time the function ends, 
  558.         one of the characters is printed again,  this time backwards 
  559.         as the string of recursive function calls is retraced.
  560.  
  561.              Don't worry about the character array defined in line 3 
  562.         or  the  other  new  material  presented  here.   After  you 
  563.         complete chapter 7 of this tutorial,  this program will make 
  564.         sense.   It  was  felt that introducing a second example  of 
  565.         recursion was important so this file is included here.
  566.  
  567.              One additional feature is built into this program.   If 
  568.         you observe the two calls to the function,  and the function 
  569.         itself, you will see that the function name is spelled three 
  570.         different  ways in the last few  characters.   The  compiler 
  571.         doesn't  care how they are spelled because it only uses  the 
  572.         first  8 characters of the function name so as far as it  is 
  573.         concerned,  the function is named "forward_".  The remaining 
  574.         characters  are simply ignored.   If your compiler uses more 
  575.         that  8 characters as being significant,  you will  need  to 
  576.         change  two  of  the  names  so that  all  three  names  are 
  577.         identical. 
  578.  
  579.              Compile and run this program and observe the results.
  580.  
  581.  
  582.  
  583.  
  584.  
  585.                                   Page 37    
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                     Chapter 5 - Functions and variables
  596.  
  597.  
  598.         PROGRAMMING EXERCISES
  599.  
  600.         1.   Rewrite  TEMPCONV.C,  from an earlier chapter, and move 
  601.              the temperature calculation to a function.
  602.  
  603.         2.   Write a program that writes your name on the monitor 10 
  604.              times by calling a function to do the writing. Move the 
  605.              called function ahead of the "main" function to see  if 
  606.              your compiler will allow it.
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.                                   Page 38    
  652.  
  653.